/* 
 * PLEASE NOTE
 * this date picker script is a customised version of a script
 * available from 123-reg.co.uk and is largely not the work of Andrew Stokes
 */ 
var picker_ui;
function raise_picker_ui(input, date_format) {
	/* Remove any old one */
	if (picker_ui && picker_ui.parentNode)
		picker_ui.parentNode.removeChild(picker_ui);

	/* Construct a new one */
	var year, month; /* Used below */
	var tr, td, a; /* Used below. */

	picker_ui = document.createElement("table");
	var thead = document.createElement("thead");

	function new_navigation_cell(caption, change) {
		td = document.createElement("td");
		a = document.createElement("a");
		a.href = "#";
		a.onclick = function() {
			month += change;
			if (month < 0) {
				year--;
				month = 11;
			} else if (month > 11) {
				year++;
				month = 0;
			}
			show_month();
			return false;
		}
		a.appendChild(document.createTextNode(caption));
		td.appendChild(a);
		return td;
	}

	/* Header row */
	tr = document.createElement("tr");

	tr.appendChild(new_navigation_cell("<", -1));

	var month_th = document.createElement("th");
	month_th.colSpan = 5;
	month_th.style.textAlign = "center";
	month_th.appendChild(document.createTextNode("Loading..."));
	tr.appendChild(month_th);

	tr.appendChild(new_navigation_cell(">", +1));

	thead.appendChild(tr);

	/* Day row */
	var days = [ "M", "T", "W", "T", "F", "S", "S" ];
	tr = document.createElement("tr");
	for ( var i in days) {
		td = document.createElement("td");
		td.appendChild(document.createTextNode(days[i]));
		tr.appendChild(td);
	}
	thead.appendChild(tr);

	picker_ui.appendChild(thead);

	/* On to the body */
	var tbody = document.createElement("tbody");
	picker_ui.appendChild(tbody);

	/* Javascript already has good date support */
	var now = new Date();
	month = now.getMonth();
	year = now.getFullYear();

	function pad_2(n) {
		n = "" + n;
		if (n.length < 2)
			n = "0" + n;
		return n;
	}
	function formatted_date(year, month, day) {
		day = pad_2(day);
		month = pad_2(month + 1);
		if (date_format == "dd/mm/yyyy") {
			return [ day, month, year ].join("/");
		} else {
			return [ year, month, day ].join("-");
		}
	}
	function show_month() {
		while (tbody.firstChild)
			tbody.removeChild(tbody.firstChild);

		month_th.firstChild.data = pad_2(month + 1) + " " + year;

		var cursor = new Date(year, month, 1);
		var start_day = cursor.getDay();

		var monday = 1; /* 0 is Sunday for Date() */

		function new_date_row() {
			tr = document.createElement("tr");
			return tr;
		}
		function new_date_cell(day_of_month) {
			td = document.createElement("td");
			if (day_of_month) {
				a = document.createElement("a");
				a.href = "#";
				a.onclick = function() {
					input.value = formatted_date(year, month, day_of_month);
					picker_ui.parentNode.removeChild(picker_ui);
				}
				a.appendChild(document.createTextNode(day_of_month));

				if (year == now.getFullYear() && month == now.getMonth()
						&& day_of_month == now.getDate())
					;

				td.appendChild(a);
			} else {
				td.appendChild(document.createTextNode("-"));
			}
			return td;
		}

		for ( var i = monday; i % 7 != start_day; i++) {
			if (i % 7 == monday)
				tbody.appendChild(new_date_row());

			tr.appendChild(new_date_cell());
		}
		while (cursor.getMonth() == month) {
			if (cursor.getDay() == monday)
				tbody.appendChild(new_date_row());

			tr.appendChild(new_date_cell(cursor.getDate()));

			/* Move on a day */
			cursor.setDate(cursor.getDate() + 1);
		}
	}
	show_month();

	/* Find out where the input is */
	var x = 0;
	var y = 0;
	for ( var e = input; e.offsetParent; e = e.offsetParent) {
		x += e.offsetLeft;
		y += e.offsetTop + 8;
	}

	picker_ui.style.position = "absolute";
	picker_ui.style.top = y + "px";
	picker_ui.style.left = x + "px";

	/* Add the CSS class */
	picker_ui.className = "calendarcontrol";
	picker_ui.id = "picker_ui";
	
	document.body.appendChild(picker_ui);
	return false;
}
